package com.hs.sfzy.controller;

/**
 *  FM音乐
 * @author zgy
 * @date 2024/10/24 12:47
 * @param null
 * @return null
 */
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Fm;
import com.hs.sfzy.service.FmService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName FmController
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月22日 21:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/music")
public class FmController {
    @Autowired
    private FmService fmService;

    /**
     *  添加数据
     * @author zgy
     * @date 2024/10/22 21:43
     * @param fm
     * @return com.zgy.sfzy.util.Result
     */
    @PostMapping
    public Result insert(@RequestBody Fm fm){
        int insert = fmService.insert(fm);
        if (insert != 0) {
            return Result.success("增加成功");
        }else {
            return Result.error("增加失败");
        }
    }

    /**
     *  根据id删除数据
     * @author zgy
     * @date 2024/10/22 21:43
     * @param id
     * @return com.zgy.sfzy.util.Result
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id){
        int delete = fmService.delete(id);
        if (delete != 0) {
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }

    }
    /**
     * 修改数据
     * @author zgy
     * @date 2024/10/24 9:47
     * @param fm
     * @return com.zgy.sfzy.util.Result
     */

    @PutMapping
    public Result update(@RequestBody Fm fm){
        int update = fmService.update(fm);
        if (update != 0) {
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }

    }

    /**
     * 根据id查询
     * @author zgy
     * @date 2024/10/24 9:47
     * @param id
     * @return com.zgy.sfzy.util.Result
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id){
        Fm fm = fmService.selectById(id);
        return Result.success(fm);
    }

    /**
     * 查询所有数据
     * @author zgy
     * @date 2024/10/24 9:48
     * @param fm
     * @return com.zgy.sfzy.util.Result
     */
    @GetMapping
    public Result selectAll(Fm fm){
        List<Fm> list = fmService.selectAll(fm);
        return Result.success(list);
    }

    /**
     *  分页查询
     * @author zgy
     * @date 2024/10/24 9:57
     * @param pageNum
     * @param pageSize
     * @param fm
     * @return com.zgy.sfzy.util.Result
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Fm fm) {
        PageInfo<Fm> info = fmService.selectByPageInfo(pageNum, pageSize, fm);
        return Result.success(info);
    }
}
